import { Application } from "jsr:@oak/oak/application";
import { errorMiddleware } from "./middleware/errorMiddleware.ts";
import { loadRoutes } from "./utils/loadRoutes.ts";
import { initializeAuth } from "./services/auth.ts";
import { getServerConfig } from "./config/config.ts";

async function startServer() {
  try {
    await initializeAuth();
    
    const app = new Application();
    const { port, host } = getServerConfig();
    
    app.use(errorMiddleware);
    await loadRoutes(app);
    
    if (import.meta.main) {
      console.log(`Server listening on http://${host}:${port}`);
      await app.listen({ port, hostname: host });
    }
  } catch (error) {
    console.error("服务器启动失败:", error);
    Deno.exit(1);
  }
}

startServer(); 